In this part of the course, we will cover the following concepts:
htmlwidgets libraryBefore moving forward, let’s review the series used in highcharter
Below are 2 columns:
Pair the correct series type and plot type, and share your answers in chat
|
Column A Highcharter series type |
Column B Plot type |
|---|---|
1 - column
|
a - density |
2 - area
|
b - bar plot |
3 - bar
|
c - horizontal bar plot |
column is for creating a bar plot (1-b)area is for a density plot (2-a)bar is for creating a horizontal bar plot (3-c)| Objective | Complete |
|---|---|
| Construct and save a boxplot and a column plot with hchart | |
| Visualize a correlation plot with hchart |
In order to maximize the efficiency of your workflow, use the box package and encode your directory structure into variables
Let the main_dir be the variable corresponding to your materials folder
data directory inside the materials folder in your environment; hence we will save their path to a data_dir variableplots directory corresponding to plot_dir variablepaste0 command and pass the strings you would like to paste togetherhealthcare-dataset-stroke-dataThe dataset has 12 characteristics (columns), of which:
data_dir into R’s environment and subset itIn this module, we will explore a subset of this data set, which includes the following variables:
bmi into a numeric column followed by imputing the missing values with the meanhcboxplot()hcboxplot() allows us to create an interactive boxplotIt needs two arguments:
x requires the numeric data to be plotted along the x-axis (hcboxplots in highcharter are horizontal by default)var requires categorical data to be plotted along the y-axishc_plotOptions() function%>%) to the original chart to enhance our base plothc_plotOptionsLet’s create an interactive multiple-column plot to compare summary by variable
We first need to get the summary statistics and save them as a separate data frame
# Create data summary.
HDS_summary = summary(HDS_subset_col)
# Save it as a data frame.
HDS_summary = as.data.frame(HDS_summary)
# Inspect the data.
head(HDS_summary) Var1 Var2 Freq
1 age Min. : 0.08
2 age 1st Qu.:25.00
3 age Median :45.00
4 age Mean :43.23
5 age 3rd Qu.:61.00
6 age Max. :82.00
# Remove an empty variable.
HDS_summary$Var1 = NULL
# Rename remaining columns.
colnames(HDS_summary) = c("Variable",
"Summary")
# Inspect updated data.
head(HDS_summary) Variable Summary
1 age Min. : 0.08
2 age 1st Qu.:25.00
3 age Median :45.00
4 age Mean :43.23
5 age 3rd Qu.:61.00
6 age Max. :82.00
separate() function# Separate `Summary` column into 2 columns.
HDS_summary = HDS_summary %>% #<- set original data
separate(Summary, #<- separate `Summary` variable
into = c("Statistic", "Value"), #<- into 2 columns: `Statistic`, `Value`
sep = ":", #<- set separating character
convert = TRUE) #<- where applicable convert data (to numeric)
# Inspect the first few entries in data.
head(HDS_summary) Variable Statistic Value
1 age Min. 0.08
2 age 1st Qu. 25.00
3 age Median 45.00
4 age Mean 43.23
5 age 3rd Qu. 61.00
6 age Max. 82.00
[1] 18
integer(0)
[1] 18
# Construct the summary chart.
HDS_summary_interactive =
hchart(HDS_summary, #<- set data
"column", #<- set type (`column` in highcharts)
hcaes(x = Statistic, #<- arrange `Statistics` across x-axis
y = Value, #<- map `Value` of each `Statistic` to y-axis
group = Variable)) #<- group columns by `Variable`tooltip to contain information about the group of variables i.e age, bmi, and avg_glucose_level rather than than the individual variablestooltip options of the chart using the hc_tooltip() optionshared option is often used to share a tooltip between members of a group| Objective | Complete |
|---|---|
| Construct and save a boxplot and a column plot with hchart |
✔ |
| Visualize a correlation plot with hchart |
hchart() a correlation matrix, it will recognize the data type and create a correlation plot in response# Compute a correlation matrix for the first
# 4 variables in our data.
cor_matrix = cor(HDS_subset_col[, 1:3])
# Construct a correlation plot by
# simply giving the plotting function
# a correlation matrix.
correlation_interactive = hchart(cor_matrix) %>%
# Add title to the plot.
hc_title(text = "HDS data: correlation")htmlwidgets# Set working directory to where you save plots.
setwd(plot_dir)
# Save desired interactive plot to an HTML file.
saveWidget(scatter_interactive, #<- plot object to save
"interactive_scatterplot.html", #<- name of file to where the plot is to be saved
selfcontained = TRUE) #<- set `selfcontained` to TRUE, so that
# all necessary files and scripts are embedded
# into the HTML file itself You are now ready to try tasks 1-9 in the Exercise for this topic
| Objective | Complete |
|---|---|
| Construct and save a boxplot and a column plot with hchart |
✔ |
| Visualize a correlation plot with hchart |
✔ |
In this part of the course, we have covered:
htmlwidgets library